﻿Imports System.Data.SqlClient
Imports System.Net.Mail

Public Class frmNewTicket
    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        Me.CombPriority.SelectedIndex = -1
        Me.CombType.SelectedIndex = -1
        Me.CombModules.SelectedIndex = -1
        Me.txtProblemDescription.Clear()
    End Sub

    Private Sub btnNew_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click
        Me.ErrProv.Clear()
        If Me.CombPriority.SelectedIndex = -1 Then
            Me.ErrProv.SetError(Me.CombPriority, "Please fill in all details")
            Me.CombPriority.Focus()

        ElseIf Me.CombType.SelectedIndex = -1 Then
            Me.ErrProv.SetError(Me.CombType, "Please fill in all details")
            Me.CombType.Focus()

        ElseIf Me.CombModules.SelectedIndex = -1 Then
            Me.ErrProv.SetError(Me.CombModules, "Please fill in all details")
            Me.CombModules.Focus()

        ElseIf Me.txtProblemDescription.Text.Trim.Length = 0 Then
            Me.ErrProv.SetError(Me.txtProblemDescription, "Please fill in all details")
            Me.txtProblemDescription.Focus()

        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand()
                Dim Trans As SqlTransaction
                Dim TicketNo As Integer
                Dim ClientName, EmailPassword As String
                Dim EmailsNotifications As New ArrayList
                Dim Reader As SqlDataReader

                cnnSupport.Open()
                Trans = cnnSupport.BeginTransaction
                cmd.Connection = cnnSupport
                cmd.Transaction = Trans

                cmd.CommandText = "Select FullName From Clients Where SNo=" & TechnicalSupportCompanyID
                ClientName = cmd.ExecuteScalar

                cmd.CommandText = "Select IsNull(Max(TicketNo),10000) From Support_Tickets Where ClientID=" & TechnicalSupportCompanyID
                TicketNo = CInt(cmd.ExecuteScalar) + 1

                cmd.CommandText = "Insert Into Support_Tickets " & _
                                  "(ClientID,TicketNo,Status,Priority,Type,Module,ProblemDescription,SubmittedBy,SubmittedOn) Values " & _
                                  "(@ClientID,@TicketNo,@Status,@Priority,@Type,@Module,@ProblemDescription,@SubmittedBy,GetDate())"

                'Add values
                cmd.Parameters.AddWithValue("@ClientID", TechnicalSupportCompanyID)
                cmd.Parameters.AddWithValue("@TicketNo", TicketNo)
                cmd.Parameters.AddWithValue("@Status", "Active")
                cmd.Parameters.AddWithValue("@Priority", Me.CombPriority.Text)
                cmd.Parameters.AddWithValue("@Type", Me.CombType.Text)
                cmd.Parameters.AddWithValue("@Module", Me.CombModules.Text)
                cmd.Parameters.AddWithValue("@ProblemDescription", Me.txtProblemDescription.Text.Trim)
                cmd.Parameters.AddWithValue("@SubmittedBy", CurrentUser)
                cmd.ExecuteNonQuery()

                'Get list of emails to be notified
                cmd.CommandText = "Select Email From Oasis_Support_Emails"
                Reader = cmd.ExecuteReader
                While Reader.Read
                    EmailsNotifications.Add(Reader.Item("Email"))
                End While
                Reader.Close()

                cmd.CommandText = "Select Pass From Support_Email_Pass"
                EmailPassword = cmd.ExecuteScalar

                Trans.Commit()
                cnnSupport.Close()

                'Notify by email
                'Send welcome notification
                Dim Subject, Body As String
                Dim Receivers As New ArrayList

                Subject = "Support: " & ClientName
                Body = "<B>New support ticket:</B><br/><br/>" & _
                       "<table border=""1"">" & _
                       "<tr><td><B>Client</B></td> <td>" & ClientName & "</td></tr>" & _
                       "<tr><td><B>Priority</B></td> <td>" & Me.CombPriority.Text & "</td></tr>" & _
                       "<tr><td><B>Type</B></td> <td>" & Me.CombType.Text & "</td></tr>" & _
                       "<tr><td><B>Module<B></td> <td>" & Me.CombModules.Text & "</td></tr>" & _
                       "<tr><td><B>Problem Description</B></td> <td>" & Me.txtProblemDescription.Text & "</td></tr>" & _
                       "<tr><td><B>Submitted By</B></td> <td>" & CurrentUser & "</td></tr>" & _
                       "</table><br/><br/>" & _
                       "Please don't reply to this email.<br/>" & _
                       "Sent from Oasis - E-Support"

                For Each Email As String In EmailsNotifications
                    Receivers.Add(Email)
                Next

                If Receivers.Count > 0 Then
                    SendSupportTicketEmail(Subject, Body, Receivers, EmailPassword)
                End If

                MsgBox("Ticket submitted successfully")

                Me.Cursor = Cursors.Default

                Me.Close()
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnnSupport.State = ConnectionState.Open Then
                    cnnSupport.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Sub CheckLength()
        Try
            Dim Length As Integer = 1000
            Length = 1000 - Me.txtProblemDescription.Text.Length

            Me.lblCharactersLeft.Text = "Characters Left: " & Length
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtProblemDescription_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtProblemDescription.TextChanged
        CheckLength()
    End Sub

    Private Sub frmNewTicket_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CheckLength()
    End Sub

    Public Sub SendSupportTicketEmail(ByVal Subject As String, ByVal Body As String, ByVal Receivers As ArrayList, ByVal Password As String)
        Try

            Dim mail As New MailMessage()

            Dim SmtpServer As New SmtpClient()
            SmtpServer.Credentials = New Net.NetworkCredential("support@oasisoft.net", Password)
            'SmtpServer.Port = 80
            SmtpServer.Host = "smtp.oasisoft.net"
            'SmtpServer.EnableSsl = True

            mail.From = New MailAddress("support@oasisoft.net", "Oasis - E-Support")

            mail.Subject = Subject
            mail.Body = Body
            mail.IsBodyHtml = True 'Change it as you need

            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

            For Each rec As String In Receivers
                mail.To.Add(rec)
            Next

            SmtpServer.Send(mail)

            mail.Dispose()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class